package com.bauer.java.spring.web;

import com.alibaba.fastjson.JSON;
import com.bauer.java.spring.bean.User;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Author ：rui.wei
 * @Date ：Created in 14:56 2019/4/18
 * @Description：
 */
@RestController
public class ParamBindController {

    @RequestMapping("/param")
    public String param(@RequestBody User user) {
        System.out.println(JSON.toJSONString(user));
        return "hello";
    }


    /**
     * 当PathVariable中未指定值时采用参数名来匹配 也是可以的
     * @param param0
     * @return
     */
    @RequestMapping("/path/{param0}/{param1}")
    public String pathValueParam(@PathVariable String param0,@PathVariable("${param1}") String param) {
        System.out.println("param0 = " +  param0 +"=====param1 = " + param0);
        return "hello";
    }


    /**
     * get请求 url:http://localhost:8080/request/param?param0=a&param1=b
     */
    @RequestMapping("/request/param")
    public String requestParam(@RequestParam(name ="param0") String param0,@RequestParam(name ="param1")  String param1,@RequestParam(value = "file",required = false) MultipartFile file) {
        System.out.println("param0 = " + param0 + "=====param1 = " + param1);
        return "hello";
    }


    @PostMapping("/request/body")
    public String requestBody(@RequestBody User user) {
        System.out.println(JSON.toJSONString(user));
        return "hello";
    }

    @RequestMapping("/param/validation")
    public String paramValidation(@RequestBody @Validated User user) {
        System.out.println(JSON.toJSONString(user));
        return "hello";
    }

}
